package sury.biz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sury.biz.service.GoodsOrderService;
import sury.dal.dto.GoodsOrderDTO;
import sury.dal.entity.GoodsEntity;
import sury.dal.entity.GoodsOrderEntity;
import sury.dal.entity.GoodsOrderGoodsEntity;
import sury.dal.mapper.GoodsMapper;
import sury.dal.mapper.GoodsOrderGoodsMapper;
import sury.dal.mapper.GoodsOrederMapper;
import sury.dal.vo.GoodsOrderVO;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class GoodsOrderServiceImpl extends ServiceImpl<GoodsOrederMapper, GoodsOrderEntity> implements GoodsOrderService {
    @Autowired
    private GoodsOrederMapper goodsOrederMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private GoodsOrderGoodsMapper goodsOrderGoodsMapper;

    @Override
    public Page<GoodsOrderEntity> pageOrder(GoodsOrderDTO goodsOrderDTO) {
        Page<GoodsOrderEntity> page=new Page<>(goodsOrderDTO.getCurrent(),goodsOrderDTO.getSize());
        Long id=goodsOrderDTO.getId();
        Integer status=goodsOrderDTO.getStatus();
        String name=goodsOrderDTO.getUsername();
        String phone=goodsOrderDTO.getPhone();
        LocalDateTime createTime=goodsOrderDTO.getCreateTime();
        return page(page, new QueryWrapper<GoodsOrderEntity>()
                        .eq(id!=null,"id",id)
                        .eq(status!=null,"status",status)
                        .like(name!=null&&!name.isEmpty(),"name",name)
                        .eq(phone!=null&&!phone.isEmpty(),"phone",phone)
                        .eq(createTime!=null,"create_time",createTime));
    }

    @Override
    public GoodsOrderVO detail(Long orderId) {
        GoodsOrderEntity goodsOrderEntity=getById(orderId);
        GoodsOrderVO goodsOrderVO= BeanUtil.copyProperties(goodsOrderEntity,GoodsOrderVO.class);

        //获取订单商品信息
        List<GoodsOrderGoodsEntity> list=goodsOrderGoodsMapper.selectList(new QueryWrapper<GoodsOrderGoodsEntity>().eq("order_id",orderId));
        goodsOrderVO.setGoodsList(list);

        return goodsOrderVO;
    }
}
